﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Newtonsoft.Json.Linq;
using Org.BouncyCastle.Utilities.Encoders;
using System.Net;
using up6.db.model;
using up6.utils;
using Up6.core.Up6Manager;
using Up6.core.Up6Manager.Models.Req;


namespace Up6.core.Controllers;

[ApiController]
[Route("")]
public class Up6Controller : ControllerBase
{
    private readonly Up6Action act;
    private readonly IConfiguration _configuration;
    public Up6Controller(Up6Action up6Service, IConfiguration configuration)
    {
        act = up6Service;
        _configuration = configuration;
    }

    [HttpGet]
    [Route("api/up6/f_create")]
    public async Task<IActionResult> create(
        [FromQuery] string id,
        [FromQuery] string uid,
        [FromQuery] string md5,
        [FromQuery] long lenLoc,
        [FromQuery] long lenLocSec,
        [FromQuery] string sizeLoc,
        [FromQuery] string callback,
        [FromQuery] string pathLoc,
        [FromQuery] int blockSizeSec=0,
        [FromQuery] int blockSize = 5242880,
        [FromQuery] string encryptAgo="",
        [FromQuery] string token="",
        [FromQuery] string pid = "",
        [FromQuery] string pidRoot = ""
        )
    {
        var f = new FileInf();
        f.pid= pid;
        f.pidRoot= pidRoot;
        f.md5 = md5;
        f.id= id;
        f.uid= uid;
        f.lenLoc= lenLoc;
        f.lenLocSec= lenLocSec;
        f.sizeLoc = sizeLoc;
        f.blockSize = blockSize;//块大小
        f.blockSizeSec = blockSizeSec;//加密块大小
        f.encryptAgo = encryptAgo;//加密算法
        f.encrypt = ConfigReader.storageEncrypt();//存储加密
        f.token = token;
        f.callback = callback;
        f.pathLoc = pathLoc;
        f.pathLoc = WebUtility.UrlDecode(pathLoc);
        f.nameLoc = Path.GetFileName(f.pathLoc);
        f.nameSvr = f.nameLoc;
        return await act.CreateAsync(f);

    }

    [HttpGet]
    [Route("api/up6/f_complete")]

    public dynamic complete(
        [FromQuery] string id,
        [FromQuery] string uid,
        [FromQuery] string callback
        )
    {
        var f = new FileInf();
        f.id = id;
        f.uid= uid;
        f.callback = callback;
        return act.complete(f);

    }

    [HttpPost]
    [Route("api/up6/f_post")]

    public async Task<IActionResult> postAsync(
        [FromHeader] string id,
        [FromHeader] string uid,
        [FromHeader] long lenSvr = 0,
        [FromHeader] long lenLoc=0,
        [FromHeader] long lenLocSec=0,
        [FromHeader] int blockIndex=1,
        [FromHeader] long blockOffset=0,
        [FromHeader] int blockSize=0,
        [FromHeader] int blockCount=1,
        [FromHeader] bool complete=false,
        [FromForm] string pathLoc = "",
        [FromForm] string pathSvr="",
        [FromForm] string pathRel="",
        [FromHeader] bool blockCpr=false,
        [FromHeader] string pid = "",
        [FromHeader] string pidRoot = "",
        [FromHeader] string object_id = "",
        [FromHeader] long blockOffsetCry = 0,
        [FromHeader] int blockSizeCry = 0,
        [FromHeader] int blockSizeCpr = 0,
        [FromHeader] string blockEncryptAgo = "",
        [FromHeader] string blockCprType = "",
        [FromHeader] string blockMd5 = "",
        [FromHeader] string token = ""
        )
    {
        var f = new FileInf();
        f.id = id;
        f.uid = uid;
        f.pid = pid;
        f.pidRoot = pidRoot;
        f.object_id = object_id;
        f.lenSvr = lenSvr;
        f.lenLoc = lenLoc;
        f.lenLocSec = lenLocSec;
        f.blockIndex = blockIndex;
        f.blockOffset = blockOffset;
        f.blockOffsetCry = blockOffsetCry;
        f.blockSize = blockSize;
        f.blockSizeSec = blockSizeCry;
        f.blockCount = blockCount;
        f.encrypt = ConfigReader.storageEncrypt();//加密存储
        f.encryptAgo = blockEncryptAgo;//加密存储算法,sm4,aes
        f.blockSizeCpr = blockSizeCpr;
        //req.blockCpr = blockCpr;
        f.blockCprType = blockCprType;
        f.blockMd5 = blockMd5;
        f.complete = complete;
        f.token = token;
        f.pathLoc = up6.utils.Base64Encoder.decode(pathLoc);
        f.pathSvr = up6.utils.Base64Encoder.decode(pathSvr);
        f.pathRel = up6.utils.Base64Encoder.decode(pathRel);
        f.nameLoc = PathTool.getName(f.pathLoc);
        f.nameSvr = f.nameLoc;

        return await act.postAsync(f, Request.Form.Files[0]);

    }

    [HttpGet]
    [Route("api/up6/clear")]

    public dynamic clear()
    {
        up6.db.database.up6.sql.SqlFile.build().Clear();
        return new ContentResult
        {
            Content = "up6_files,up6_folders表清空成功",
            ContentType = "application/json; charset=utf-8",
            StatusCode = 200
        };

    }

    [HttpGet]
    [Route("api/up6/f_del")]

    public dynamic del(
        [FromQuery] string id,
        [FromQuery] string uid,
        [FromQuery] string callback
        )
    {
        var f = new FileInf();
        f.id= id;
        f.uid= uid;
        f.callback= callback;
        return act.del(f);

    }

    [HttpGet]
    [Route("api/up6/f_list")]

    public dynamic list(
        [FromQuery] string uid,
        [FromQuery] string callback
        )
    {
        var f = new FileInf();
        f.uid= uid;
        f.callback = callback;
        return act.list(f);

    }

    [HttpGet]
    [Route("api/up6/f_process")]

    public dynamic process(
        [FromQuery] string id,
        [FromQuery] string uid,
        [FromQuery] long offset,
        [FromQuery] long lenSvr,
        [FromQuery] string perSvr,
        [FromQuery] string callback
        )
    {
        var f = new FileInf();
        f.id= id;
        f.uid = uid;
        f.offset= offset;
        f.lenSvr= lenSvr;
        f.perSvr= perSvr;
        f.callback = callback;
        return act.process(f);

    }

    [HttpGet]
    [Route("api/up6/fd_create")]

    public dynamic fd_create(
        [FromQuery] string id,
        [FromQuery] string uid,
        [FromQuery] long lenLoc,
        [FromQuery] string sizeLoc,
        [FromQuery] string pathLoc,
        [FromQuery] string callback,
        [FromQuery] string pid = "",
        [FromQuery] string pidRoot = ""
        )
    {
        var f = new FileInf();
        f.id= id;
        f.pid= pid;
        f.pidRoot= pidRoot;
        f.fdTask = true;
        f.uid = uid;
        f.pathLoc=WebUtility.UrlDecode(pathLoc);
        f.nameLoc = Path.GetFileName(f.pathLoc);
        f.nameSvr = f.nameLoc;
        f.lenLoc= lenLoc;
        f.callback= callback;
        return act.fd_create(f);
    }

    [HttpGet]
    [Route("api/up6/fd_complete")]

    public dynamic fd_complete(
        [FromQuery] string id,
        [FromQuery] string uid,
        [FromQuery] string callback
        )
    {
        var f = new FileInf();
        f.id = id;
        f.uid = uid;
        f.callback = callback;
        return act.fd_complete(f);
    }

    [HttpGet]
    [Route("api/up6/fd_del")]

    public dynamic fd_del(
        [FromQuery] string id,
        [FromQuery] string uid,
        [FromQuery] string callback
        )
    {
        var f = new FileInf();
        f.id = id;
        f.uid = uid;
        f.callback = callback;
        return act.fd_del(f);

    }
}
